package org.linlinjava.litemall.brand.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 商户:引用?
 */
@RestController
@RequestMapping("/brand/quote")
public class BrandQuoteController {
    @Autowired
    LitemallQuoteService quoteService;
    @Autowired
    LitemallServiceService serviceService;
    @Autowired
    private LitemallCommodityService commodityService;
    @GetMapping("list")
    public Object list(@LoginUser Integer userId) {
        Integer brandId = serviceService.getBelong(userId);
        return ResponseUtil.ok(quoteService.getAllByBrand(brandId));
    }

    @GetMapping("view")
    public Object view(@LoginUser Integer userId, @RequestParam(name = "id") Integer layout) {
        Integer brandId = serviceService.getBelong(userId);
        return ResponseUtil.ok(quoteService.findByPid(layout, brandId));
    }

    @PostMapping("addLayout")
    public Object addLayout(@LoginUser Integer userId, @RequestBody String msg) {
        Integer brandId = serviceService.getBelong(userId);
        Integer pid = JacksonUtil.parseInteger(msg, "pid");
        String name = JacksonUtil.parseString(msg, "name");
        if (StringUtils.isEmpty(name) || pid == null) {
            return ResponseUtil.badArgument();
        }
        if (pid != 0) {
            LitemallQuoteLayout layout = quoteService.findLayout(pid);
            if (layout == null || !layout.getBrandId().equals(brandId)) {
                return ResponseUtil.badArgumentValue();
            }
        }
        LitemallQuoteLayout layout = new LitemallQuoteLayout();
        layout.setBrandId(brandId);
        layout.setPid(pid);
        layout.setQuoteName(name);
        try {
            quoteService.addLayout(layout);
            return ResponseUtil.ok(layout.getId());
        } catch (Exception e) {
            return ResponseUtil.fail(403, e.getMessage());
        }
    }

    @PostMapping("updateLayout")
    public Object updateLayout(@LoginUser Integer userId, @RequestBody String msg) {
        Integer brandId = serviceService.getBelong(userId);
        Integer id = JacksonUtil.parseInteger(msg, "id");
        Integer pid = JacksonUtil.parseInteger(msg, "pid");
        String name = JacksonUtil.parseString(msg, "name");
        if (StringUtils.isEmpty(name) || pid == null || id == null) {
            return ResponseUtil.badArgument();
        }
        LitemallQuoteLayout upLayout = quoteService.findLayout(id);
        if (upLayout == null || !upLayout.getBrandId().equals(brandId)) {
            return ResponseUtil.badArgumentValue();
        }
        upLayout.setPid(pid);
        upLayout.setQuoteName(name);
        if (pid != 0) {
            LitemallQuoteLayout layout = quoteService.findLayout(pid);
            if (layout == null || !layout.getBrandId().equals(brandId)) {
                return ResponseUtil.badArgumentValue();
            }
            boolean check = quoteService.checkLoop(upLayout);
            if (!check) {
                return ResponseUtil.badArgumentValue();
            }
        }
        try {
            quoteService.updateByPrimaryKey(upLayout);
            return ResponseUtil.ok();
        } catch (Exception e) {
            return ResponseUtil.fail(403, e.getMessage());
        }

    }

    @PostMapping("deleteLayout")
    public Object deleteLayout(@LoginUser Integer userId, @RequestBody String msg) {
        Integer brandId = serviceService.getBelong(userId);
        Integer id = JacksonUtil.parseInteger(msg, "id");
        if (id == null || id < 1) {
            return ResponseUtil.badArgument();
        }
        LitemallQuoteLayout layout = new LitemallQuoteLayout();
        layout.setId(id);
        layout.setBrandId(brandId);
        quoteService.delete(layout);
        return ResponseUtil.ok();
    }

    @PostMapping("addCommodity")
    public Object addCommodity(@LoginUser Integer userId, @RequestBody String msg) {
        Integer brandId = serviceService.getBelong(userId);
        Integer pid = JacksonUtil.parseInteger(msg, "pid");
        Integer commodityId = JacksonUtil.parseInteger(msg, "commodityId");
        BigDecimal price = JacksonUtil.parseDecimal(msg, "price");
        if (pid == null || commodityId == null) {
            return ResponseUtil.badArgument();
        }
        LitemallCommodity tempCmd = commodityService.selectByIdSimple(commodityId);
        LitemallQuoteLayout layout = quoteService.findLayout(pid);
        if (tempCmd == null || layout == null) {
            return ResponseUtil.fail();
        }
        if (!tempCmd.getBrandId().equals(brandId)|| !layout.getBrandId().equals(brandId)) {
            return ResponseUtil.unauthz();
        }
        if (price == null || price.compareTo(new BigDecimal(0)) < 0) {
            return ResponseUtil.fail();
        }
        LitemallQuoteCommodity cmd = new LitemallQuoteCommodity();
        cmd.setCommodityId(commodityId);
        cmd.setPid(pid);
        cmd.setPrice(price);
        quoteService.addCommodity(cmd);
        return ResponseUtil.ok();
    }

    @PostMapping("updateCommodity")
    public Object updateCommodity(@LoginUser Integer userId, @RequestBody String msg) {
        Integer brandId = serviceService.getBelong(userId);
        Integer id = JacksonUtil.parseInteger(msg, "id");
        Integer pid = JacksonUtil.parseInteger(msg, "pid");
        Integer commodityId = JacksonUtil.parseInteger(msg, "commodityId");
        BigDecimal price = JacksonUtil.parseDecimal(msg, "price");
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        if (pid != null) {
            LitemallQuoteLayout layout = quoteService.findLayout(pid);
            if (layout == null || !layout.getBrandId().equals(brandId)) {
                return ResponseUtil.badArgumentValue();
            }
        }
        if (commodityId != null) {
            LitemallCommodity tempCmd = commodityService.selectByIdSimple(commodityId);

            if (tempCmd == null || !tempCmd.getBrandId().equals(brandId)) {
                return ResponseUtil.badArgumentValue();
            }
        }

        if (price != null && price.compareTo(new BigDecimal(0)) < 0) {
            return ResponseUtil.fail();
        }
        LitemallQuoteCommodity cmd = quoteService.findCommodity(id);
        if (cmd == null) {
            return ResponseUtil.badArgumentValue();
        }
        cmd.setPrice(price);
        cmd.setPid(pid);
        cmd.setCommodityId(commodityId);
        quoteService.updateCommodity(cmd);
        return ResponseUtil.ok();
    }

    @PostMapping("delCommodity")
    public Object delCommodity(@LoginUser Integer userId, @RequestBody String msg) {
        Integer brandId = serviceService.getBelong(userId);
        Integer id = JacksonUtil.parseInteger(msg, "id");
        if (id == null || id < 1) {
            return ResponseUtil.badArgument();
        }
        LitemallQuoteCommodity commodity = quoteService.findCommodity(id);
        if (commodity == null) {
            return ResponseUtil.badArgumentValue();
        }
        LitemallQuoteLayout layout = quoteService.findLayout(commodity.getPid());
        if (layout == null || !layout.getBrandId().equals(brandId)) {
            return ResponseUtil.unauthz();
        }
        quoteService.deleteCommodity(id);
        return ResponseUtil.ok();
    }

    @PostMapping("addByExcel")
    public Object addByExcel(@RequestParam(value = "file")MultipartFile file, @LoginUser Integer userId) {
        try {
            Integer brandId = serviceService.getBelong(userId);
            Workbook workbook = WorkbookFactory.create(file.getInputStream());
            return ResponseUtil.ok(quoteService.updateByXml(brandId, workbook.getSheetAt(0)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(456, e.getMessage());
        }
    }
}
